package org.cgs.web.beans;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public final class School implements Comparable<School>, Cloneable
{
	// Attributes for the School object
	private String name;
	private String courseCode;
	private String contact;
	private String phone;
	private String address;
	private String details;
	private int numStudents;
	private boolean farSchool;
	private boolean farSchoolSet;
	private List<String> times;

	// / Attributes for scheduling
	private boolean vehicleConstraint;
	private boolean teachingConstraint;
	private boolean backgroundConstraint;
	private int count;
	private List<Student> students = new ArrayList<Student>();

	public School()
	{
		name = "";
		courseCode = "";
		contact = "";
		phone = "";
		address = "";
		details = "";
		numStudents = 0;
		farSchool = false;
		farSchoolSet = false;
		times = new ArrayList<String>();
		vehicleConstraint = false;
		teachingConstraint = false;
		backgroundConstraint = false;
		count = 0;
	}

	public School(String schoolName, String theCourseCode, String schoolContact, String schoolPhone, int numStudentsRequested, String schoolAddress, String schoolDetails, boolean isFar, List<String> schoolTimes)
	{
		name = schoolName;
		courseCode = theCourseCode;
		contact = schoolContact;
		phone = schoolPhone;
		numStudents = numStudentsRequested;
		address = schoolAddress;
		details = schoolDetails;
		farSchool = isFar;
		farSchoolSet = true;
		times = schoolTimes;
		vehicleConstraint = false;
		teachingConstraint = false;
		backgroundConstraint = false;
		count = 0;
	} // End constructor
	
	// 
	// Get/Set methods
	//
	
	public String getAddress()
	{
		return address;
	} // End method

	public String getContact()
	{
		return contact;
	} // End method

	public int getCount()
	{
		return count;
	} // End method

	public String getCourseCode()
	{
		return courseCode;
	} // End method

	public String getName()
	{
		return name;
	} // End method

	public int getNumStudents()
	{
		return numStudents;
	} // End method

	public String getPhone()
	{
		return phone;
	} // End method

	public List<Student> getStudents()
	{
		return students;
	} // End method

	public String getTimeDetails()
	{
		return details;
	} // End method

	public List<String> getTimes()
	{
		return times;
	} // End method
	
	public boolean isFarSchool()
	{
		return farSchool;
	} // End method
	
	public boolean isFarSchoolSet()
	{
		return farSchoolSet;
	} // End method
	
	public boolean isBackgroundConstraint()
	{
		return backgroundConstraint;
	} // End method

	public boolean isTeachingConstraint()
	{
		return teachingConstraint;
	} // End method

	public boolean isVehicleConstraint()
	{
		return vehicleConstraint;
	} // End method

	public void setAddress(String a)
	{
		address = a;
	} // End method

	public void setContact(String c)
	{
		contact = c;
	} // End method

	public void setCourseCode(String c)
	{
		courseCode = c;
	} // End method

	public void setFarSchool(boolean f)
	{
		farSchool = f;
	} // End method

	public void setName(String n)
	{
		name = n;
	} // End method

	public void setNumStudents(int n)
	{
		numStudents = n;
	} // End method

	public void setPhone(String p)
	{
		phone = p;
	} // End method

	public void setTimeDetails(String d)
	{
		details = d;
	} // End method

	public void setTimes(List<String> t)
	{
		times = t;
	} // End method
	
	public void setBackgroundConstraint(boolean b)
	{
		backgroundConstraint = b;
	} // End method
	
	public void setTeachingConstraint(boolean t)
	{
		teachingConstraint = t;
	} // End method

	public void setVehicleConstraint(boolean v)
	{
		vehicleConstraint = v;
	} // End method
	
	public void setFarSchoolSet(boolean f)
	{
		farSchoolSet = f;
	} // End method	

	//
	// Override methods
	//
	
	@Override
	public int hashCode()
	{
		return name.hashCode();
	} // End method

	@Override
	public String toString()
	{
		return name;
	} // End method
	
	@Override
	public Object clone()
	{
		return new School(name, courseCode, contact, phone, numStudents, address, details, farSchool, times);
	} // End method

	@Override
	public boolean equals(Object o)
	{
		if (o instanceof School)
		{
			School so = (School) o;
			return so.name.equals(name) && so.courseCode.equals(courseCode);
		} // End if

		return false;
	} // End method
	
	public int compareTo(School s)
	{
		return name.compareTo(s.name);
	} // End method
	
	//
	// Operational methods
	//
	
	public void incrementCount()
	{
		count++;
	} // End method	

	public void decrementCount()
	{
		count--;
	} // End method

	public void resetCount()
	{
		count = 0;
	} // End method
	
	public void addStudent(Student s)
	{
		students.add(s);
	} // End method

	public void addTime(String t)
	{
		times.add(t);
	} // End method
	
	public void clearStudents()
	{
		students = new ArrayList<Student>();
	} // End method
	
	public void clearBackgroundConstraint()
	{
		backgroundConstraint = false;
	} // End method

	public void clearTeachingConstraint()
	{
		teachingConstraint = false;
	} // End method

	public void clearVehicleConstraint()
	{
		vehicleConstraint = false;
	} // End method
	
	public Node getSchoolAsXML(Document doc, Element e)
	{
		Element school = doc.createElement("School");
		school.setAttribute("IsFar", Boolean.toString(farSchool));
		school.setAttribute("NumberOfStudentsRequested", Integer.toString(numStudents));
		e.appendChild(school);

		Element nameElm = doc.createElement("Name");
		school.appendChild(nameElm);
		Text txtName = doc.createTextNode(name == null ? "" : name);
		nameElm.appendChild(txtName);

		Element contactElm = doc.createElement("Contact");
		school.appendChild(contactElm);
		Text txtContact = doc.createTextNode(contact == null ? "" : contact);
		contactElm.appendChild(txtContact);

		Element phoneElm = doc.createElement("Phone");
		school.appendChild(phoneElm);
		Text txtPhone = doc.createTextNode(phone == null ? "" : phone);
		phoneElm.appendChild(txtPhone);

		Element addressElm = doc.createElement("Address");
		school.appendChild(addressElm);
		Text txtAddress = doc.createTextNode(address == null ? "" : address);
		addressElm.appendChild(txtAddress);

		return school;
	} // End method
} // End class
